public class Leetcode0406 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        Leetcode0406 leetcode0406 = new Leetcode0406();
        TreeNode res = leetcode0406.inorderSuccessor(root, root.left);
        System.out.println(res);
    }

    TreeNode parent = null;
    TreeNode current = null;

    TreeNode res = null;

    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        inorder(root, p);
        return res;
    }

    public void inorder(TreeNode root, TreeNode p) {
        if (parent != null && parent.val == p.val) {
            res = current;
            return;
        }

        if (root == null) {
            return;
        }

        inorder(root.left, p);
        parent = current;
        current = root;
        inorder(root.right, p);
    }


}
